﻿using System;
using System.Web.Mvc;
using Products.DataAcccess;
using Products.Databases;
using Products.Databases.DataAccess;

namespace Ornament.MVCWebFrame.Areas.DataBase.Controllers
{
    public class FieldController : Controller
    {
        //
        // GET: /Field/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Field/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Field/Create id is the tableId
        public ActionResult Create(Guid? id)
        {
            if (id == null)
                throw new ArgumentNullException("id", "Table's id is empty.");
            ViewData["Table"] = (new TableAccess()).Get(id.Value);
            return View();
        }

        //
        // POST: /Field/Create

        [HttpPost]
        public ActionResult Create(Field field)
        {
            if (ModelState.IsValid)
            {
                (new FieldDao()).SaveOrUpdate(field);
                return RedirectToAction("Details", "Table", new { tableName = field.Table.Name });
            }
            return View(field);
        }

        //
        // GET: /Field/Edit/5

        public ActionResult Edit(Guid? id)
        {
            var fieldSnapshot = (new FieldSnapshotDao()).Get(id.Value);
            var tableAccess = new TableAccess();
            ViewData["Table"] = tableAccess.Get(fieldSnapshot);
            return View(fieldSnapshot);
        }

        //
        // POST: /Field/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(FieldSnapshot fieldSnapshot)
        {
            try
            {
                (new FieldSnapshotDao()).SaveOrUpdate(fieldSnapshot);
                return RedirectToAction("Details", "Table", new
                                                                {
                                                                    table = fieldSnapshot.Field.Table.ConceptName,
                                                                    version = fieldSnapshot.Version,
                                                                    product = fieldSnapshot.Field.Table.Product.Name
                                                                });
            }
            catch
            {
                return View(fieldSnapshot);
            }
        }

        public ActionResult NewChange(Guid? id)
        {
            var field = (new FieldDao()).Get(id.Value);
            ViewData["Versions"] = (new VersionDao()).GetNewChangeVersion(field.Table.Product, field);
            return View(field);
        }
        [HttpPost]
        public ActionResult NewChange(Guid? id, FormCollection collection)
        {
            var field = (new FieldDao()).Get(id.Value);
            var version = (new VersionDao()).Get(new Guid(collection["versionId"]));
            bool deleted = !String.IsNullOrEmpty(collection["deletedCheckBox"]);
            field.NewChange(version, deleted);
            if (!deleted)
            {
                field.Memo = collection["Memo"];
                field.Name = collection["Name"];
                field.Length = Convert.ToInt32(collection["length"]);
                field.SqlType = collection["SqlType"];
            }
            (new FieldDao()).SaveOrUpdate(field);
            return RedirectToAction("Details", "Table", new { id = field.Table.Id });
        }

        //
        // GET: /Field/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Field/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
